/*
 * Copyright (C) 2021, All rights Reserved, Designed By
 * @author: maoyz
 * @Copyright: 2021-02-25 10:12  Inc. All rights reserved.
 */
package com.myz.cloud.stream.rocketmq.header.producer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.myz.cloud.stream.rocketmq.header.channel.IHeaderMessageChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.MimeTypeUtils;

/**
 * @author maoyz
 */
@EnableBinding({IHeaderMessageChannel.class})
public class HeaderProducerManager implements CommandLineRunner {

    @Autowired
    private IHeaderMessageChannel headerMessageChannel;

    private ObjectMapper objectMapper = new ObjectMapper();


    @Override
    public void run(String... args) throws Exception {

        int count = 5;
        for (int index = 1; index <= count; index++) {
            this.headerMessageChannel.headerOutput().send(MessageBuilder.withPayload("msg-" + index)
                    .setHeader("index", index).build());

        }

        this.headerMessageChannel.headerOutput().send(MessageBuilder
                .withPayload(objectMapper.writeValueAsString(new Person(99, "Jim")))
                .setHeader("index", 9999)
                .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
                .build());

    }

    public static class Person {
        int age;
        String name;

        public Person() {
        }

        public Person(int age, String name) {
            this.age = age;
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Person{" + "age=" + age + ", name='" + name + '\'' + '}';
        }
    }
}